package com.anlogic.sdk.templates.core;

import java.util.HashMap;
import java.util.Map;
import java.util.regex.Pattern;

import org.apache.logging.log4j.Logger;
import org.eclipse.cdt.managedbuilder.core.IConfiguration;
import org.eclipse.core.runtime.Platform;

import com.anlogic.sdk.utils.LogUtils;

public class Utils {
	private static Logger logger = LogUtils.getLogger(Utils.class);
	
	public static final String VARIABLE_IDE_ROOT_PATH = "IDE_ROOT_PATH";
	public static final String VARIABLE_ECLIPSE_HOME = "eclipse_home";
	public static final String VARIABLE_EXECUTABLE_SUFFIX = "executable_suffix";
	public static final String VARIABLE_OS_TYPE = "os_type";
	
	public static Map<String, String> macroMap = new HashMap<String, String>();
	
	static {
		macroMap.put(VARIABLE_IDE_ROOT_PATH, getIDERootPath());
		macroMap.put(VARIABLE_ECLIPSE_HOME, eclipseHome());
		macroMap.put(VARIABLE_EXECUTABLE_SUFFIX, executableSuffix());
		macroMap.put(VARIABLE_OS_TYPE, osType());
	}
	
	public static boolean isBuildType(IConfiguration config, String buildTypeSuffix) {

		if (buildTypeSuffix == null)
			return true;

		// If suffix not given, assume it is ok
		buildTypeSuffix = buildTypeSuffix.trim();
		if (buildTypeSuffix.length() == 0)
			return true;

		String configBuildTypeValue = config.getBuildProperties().getProperty("org.eclipse.cdt.build.core.buildType")
				.getValue().toString();
		if (configBuildTypeValue != null && configBuildTypeValue.endsWith(buildTypeSuffix)) {
			return true;
		}

		return false;
	}

	public static boolean isConditionSatisfied(String condition) {

		if (condition == null)
			return true;

		condition = condition.trim();
		if (condition.length() == 0) {
			// No condition string present, take it as ALWAYS
			return true;
		}
		
		condition = resolveDynamicVariable(condition);

		String sa[] = condition.split(" ");
		if (sa.length != 3) {
			logger.error("Unrecognised condition " + condition);
			return false;
		}
		
		if ("==".equals(sa[1])) {
			return sa[0].equals(sa[2]);
		} else if ("!=".equals(sa[1])) {
			return !sa[0].equals(sa[2]);
		}

		return false;
	}
	
	public static String osType() {
	    String os = Platform.getOS();
	    if (Platform.OS_LINUX.equals(os)) {
	    	return Platform.OS_LINUX;
	    } else if (Platform.OS_MACOSX.equals(os)) {
	        return Platform.OS_MACOSX;
	    } else if (Platform.OS_WIN32.equals(os)) {
	    	return Platform.OS_WIN32;
	    }
	    return Platform.OS_UNKNOWN;
	}
	
	public static String osName() {
		String osNameString = System.getProperty("os.name");
		return osNameString;
	}
	
	public static boolean isWindows() {
		return osName().toLowerCase().contains("windows");
	}
	
	public static boolean isLinux() {
		return osName().toLowerCase().contains("linux");
	}
	
	public static boolean isHidden(String path) {
		String splitPattern = Pattern.quote(System.getProperty("file.separator")) ;
		String path_list[] = path.split(splitPattern);
		for(String item: path_list) {
			if (item.equals(".") || item.equals("..")) {
				continue;
			}
			if (item.indexOf(".") == 0 && item.length() > 1) {
				return true;
			}
		}
		return false;
	}
	
	public static String eclipseHome() {
		String eclipseHomeString = System.getProperty("eclipse.home.location"); //file:/path
		if (isWindows()) {
			eclipseHomeString = eclipseHomeString.substring(6);
		}
		if (isLinux()) {
			eclipseHomeString = eclipseHomeString.substring(5);
		}
		return eclipseHomeString;
	}
	
	public static String getIDERootPath() {
		return System.getenv("IDE_ROOT_PATH");
	}
	
	public static String executableSuffix() {
		String executableSuffixString = "";
		if (isWindows()) {
			executableSuffixString = ".exe";
		}
		if (isLinux()) {
			executableSuffixString = "";
		}
		return executableSuffixString;
	}
	
	public static String locationPath() {
		return System.getProperty("user.dir");
	}
	
	public static String getRealFilePath(String path) {
		String FILE_SEPARATOR = System.getProperty("file.separator");
		return path.replace("/", FILE_SEPARATOR).replace("\\", FILE_SEPARATOR);
	}
	
	public static HashMap<String, String> getHasMap(String[] stringList) {
		HashMap<String, String> map = new HashMap<String, String>();
		
		for (String item:stringList) {
			String subArr[] = item.split("=");
			map.put(subArr[0].trim(), subArr[2].trim());
		}
		
		return map;
	}
	
	public static String resolveDynamicVariable(String input) {
		String output = input;
		if (input.indexOf("${") >= 0) {
			for (String key : macroMap.keySet()) {
				if (input.indexOf("${" + key + "}") >= 0) {
					output = output.replaceAll("[$][{]" + key + "[}]", macroMap.get(key));
					break;
				}
			}
		}
		return output;
	}
}
